package utils;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;

public class Utils {
	public static String runCmd(String cmd) {

		try {
			Runtime run = Runtime.getRuntime();
			Process pr;
			pr = run.exec(cmd);
			pr.waitFor();
			BufferedReader buf = new BufferedReader(new InputStreamReader(
					pr.getInputStream()));
			String line;
			String out = "";
			while ((line = buf.readLine()) != null) {
				out += line + "\n";
			}
			return out;

		} catch (IOException e) {
			e.printStackTrace();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		return null;

	}

	public static boolean finished() {
		File lockFile = new File("log/.lock");
		return !lockFile.exists();
	}
	
	public static void writeFile(File file, String line) {
		if (!file.exists()) {
			try {
				file.createNewFile();
			} catch (IOException e) {
				throw new RuntimeException("Log file " + file.getName() + " cannot be created.");
			}
		}

		try {
			BufferedWriter bw = new BufferedWriter(new FileWriter(file, true));
			bw.write(line);
			bw.newLine();
			bw.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
